#include "utils.hpp"
#define DEVICENUM 0
#define min(x, y) (x) <= (y) ? (x) : (y);

int main(int argc, char **argv)
{
    using namespace CPUCUDATools;
    cudaSetDevice(DEVICENUM);

    cv::Size test_size(640, 640);
    const int run_times = 1000; // 执行总次数，并计算平均时间
    const std::string image_path = "images/demo_960x540.png";
    cv::Mat src_image = cv::imread(image_path);

    float sx = test_size.width / (float)src_image.cols;
    float sy = test_size.height / (float)src_image.rows;
    float scale = min(sx, sy);

    float i2d_matrix_values[] = {
        scale,
        0,
        -scale * src_image.cols * 0.5f + test_size.width * 0.5f + scale * 0.5f - 0.5f,
        0,
        scale,
        -scale * src_image.rows * 0.5f + test_size.height * 0.5f + scale * 0.5f - 0.5f,
    };

    // 这两个可以预先算好，如果尺寸不固定，可以遍历要输入的图片尺寸，做成一个表格，采用查表法提速
    cv::Mat_<float> i2d_matrix(2, 3), d2i_matrix(2, 3);
    memcpy(i2d_matrix.ptr<float>(0), i2d_matrix_values, sizeof(i2d_matrix_values)); // 2行3列的M矩阵赋值
    cv::invertAffineTransform(i2d_matrix, d2i_matrix);                              // 2行3列M的逆矩阵求解

    // 1. 使用cuda自定义的warpAffine+focus融合，其中resize是双线性差值方式
    // float *resize_data_host = (float *)malloc(test_size.width * test_size.height * 3 * sizeof(float));
    float *resize_data_host = (float *)malloc(test_size.width / 2 * test_size.height / 2 * 12 * sizeof(float)); // shape->(1,12,320,320)
    cuda_warp(src_image, resize_data_host, d2i_matrix, test_size, run_times);

    free(resize_data_host);

    return 0;
}